<template>
  <div class="login-container">
    <el-form
      ref="loginFormRules"
      :rules="loginFormRules"
      :model="formData"
      class="login-form"
      @keyup.enter.native="loginHandle"
    >
      <div class="title">
        <h3>vue2.x后台系统快速开发</h3>
      </div>
      <el-form-item prop="username">
        <el-input v-model="formData.username">
          <i slot="prefix" class="el-input__icon fa fa-user"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="formData.password" type="password">
          <i slot="prefix" class="el-input__icon fa fa-key"></i>
        </el-input>
      </el-form-item>
      <el-button type="primary" @click="loginHandle">登录</el-button>
    </el-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  data () {
    return {
      formData: {
        username: 'yezhiyuan',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapActions('login', ['login']),
    /* 用户登录 */
    loginHandle () {
      this.$refs.loginFormRules.validate(valid => {
        if (!valid) return false
        /* 请求登录接口 */
        this.login(this.formData).then(res => {
          window.sessionStorage.setItem('token', res)
          this.$router.push('/home')
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  min-height: 100vh;
  background-color: $color-aside-bg;
  .login-form {
    position: absolute;
    width: 400px;
    max-width: 100%;
    top: 40%;
    left: 50%;
    transform: translate(-50%, -50%);
    .title h3 {
      text-align: center;
      margin-bottom: 40px;
      font-size: 36px;
      color: $color-info-light;
      font-weight: 700;
      letter-spacing: 10px;
    }
    .el-button {
      width: 100%;
      background-color: $color-primary;
      margin-top: 20px;
      &:hover {
        background-color: $color-primary-dark;
      }
    }
    .el-form-item {
      background: rgba(0, 0, 0, 0.1);
      border: 1px solid hsla(0, 0%, 100%, 0.1);
      border-radius: 5px;
      .el-input {
        /deep/ .el-input__inner {
          background: transparent;
          color: $color-white;
          border: none;
        }
        /deep/ .el-input__prefix {
          display: flex;
          align-items: center;
        }
      }
    }
  }
}
</style>
